package deamon;

import java.util.Scanner;

/**
 * todo
 *
 * @author: luolidong
 * @create: 2021-07-11 16:52
 * @version: 1.0
 */
public class Daemon {
    public static void main(String[] args) {
        Thread thread = new Thread(new DaemonRunner(), "DaemonRunner");
        thread.setDaemon(true);
        thread.start();
        for (int i = 0; i <10 ; i++) {
            System.out.println("11111111");
        }
    }
/* 在构建Daemon线程时，不能依靠finally块中的内容来确保执行关闭或清理资源
的逻辑。在非守护线程执行完，jvm立即终止程序*/
    static class DaemonRunner implements Runnable {
        @Override
        public void run() {
            Scanner scanner=null;
            try {
                scanner = new Scanner(System.in);
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                System.out.println("DaemonThread finally run.");
                assert scanner != null;
                scanner.close();
            }
        }
    }
}
